﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using WebApiV4Test.Data;
using WebApiV4Test.Data.Entity;
using WebApiV4Test.Domain;

namespace WebApiV4Test.Utils
{
    public class DbInitialize
    {
        public static void seed(IServiceProvider service)
        {
            using (var scope = service.CreateScope())
            {
                var db = scope.ServiceProvider.GetService(typeof(Abmin1024Context)) as Abmin1024Context;

                var _user = scope.ServiceProvider.GetService(typeof(IRespository<Users>)) as EFResPository<Users>;
                var _role = scope.ServiceProvider.GetService(typeof(IRespository<Roles>)) as EFResPository<Roles>;

                db.Database.EnsureCreated();

                var haser = db.Users.Any();
                if (!haser)
                {
                    var role = new Roles
                    {
                        RoleName = "管理员",
                        Description = "root"
                    };
                    _role.Insert(role);

                    _user.InsertBulk(new Users[]
                    {
                        new Users
                        {
                            UserName="aaa",
                            Password="123",
                            RolesId=role.Id
                        },
                        new Users
                        {
                            UserName="bbb",
                            Password="123",
                            RolesId=role.Id
                        },
                    });
                }

            }
        }
    }
}
